<?php
/**
 * The form for updating a machine.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-11-20
 */
require_once "login_info.php";
require_once "machines.php";
loginInfo::update_login();
?>

<html>
	<head>
		<title>Opol - Update Machine</title>
	</head>
	<body>
		<link rel="stylesheet" type="text/css" href="opol.css">
		<?php require 'menu.php' ?>
		<br>
<?php
	if ($_POST) {
		if (isset ($_POST['mach_id'])) {
			$db = new database();

			// Update the machine's informachion, and move on.
			$mach = new machine ((int)$_POST['mach_id']);
			$mach->init_from_post ($_POST);
			$result = $mach->update_database_by_id ($db);

			// Offer the option to move on.

			if (!$result)
				echo "You do not have sufficient permissions to perform this action.<br>\n";
			else
				echo "Database Successfully Updated!<br>\n";

			echo "<a href=\"/\">Back to main menu.</a><br>\n";

		} else {
			$mach_id = $_POST['name'];
			if (!$mach_id) {
			} else {
				$db = new database ();
				$mach = new machine ($mach_id);
				$ret = $mach->init ($db);
				if (!$ret) {
					echo "Error<br>\n";
				}

				$db->close ();
			}
		}

		$name = $mach->name;
		$type = $mach->type;
		$cost = $mach->cost;

	} else {
		header ("location: update_entry.php?type=machine");
	}
?>
		<form name="machine" action="" method="post">
			<input type="hidden" name="mach_id" value="<?php print $mach_id;?>">
			<table>
				<tr>
					<td><label>Name:</label></td>
					<td><input type="text" name="name" value="<?php print $name;?>"></td>
			   </tr>
			   <tr>
					<td><label>Type:</label></td>
					<td><input type="text" name="type" value="<?php print $type;?>"></td>
			   </tr>

			   <tr>
					<td><label>Cost:</label></td>
					<td><input type="text" name="cost" value="<?php print $cost;?>"></td>
			   </tr>
	
			   <tr>
					<td><input type="submit" value="Update"></td>
			   </tr>
			</table>
		</form>
	</body>
</html>
